module Project where
import Sala
import SalaEncja
import Seans
import Rezerwacja
import Sprzedaz
import Miejsce
--end of import

f = putStr "\nHello\n\n"

type ID = Int
type Attrib = (String, String)

class Objects o where
	object	::	ID	->	[Attrib]	-> o
	getID	::	o	->	ID
	getAtts	::	o	->	[Attrib]
	getName	::	o	->	String
	getName	=	snd	. head . filter (("name"==) . fst) . getAtts

class (Objects o) => Databases d o where
	empty :: d o
	getLastID :: d o -> ID
	getObjects :: d o -> [o]
	setLastID :: ID -> d o -> d o
	setObjects :: [o] -> d o -> d o

	insert :: [Attrib] -> d o -> d o
	insert as db = setLastID i' db' where
		db' = setObjects os' db
		os' = o : os
		os = getObjects db
		o = object i' as
		i' = 1 + getLastID db

	select :: ID -> d o -> o
	select i = head . filter ((i==).getID) . getObjects

	selectBy :: (o -> Bool) -> d o -> [o]
	selectBy f = filter f . getObjects	

data Object = Obj ID [Attrib] deriving Show

instance Objects Object where
	object i as =	Obj i as
	getID (Obj i as) = i
	getAtts	(Obj i as) = as
	
data DBS o = DB ID [o] deriving Show

instance Databases DBS Object where
	empty = DB 0 []
	getLastID (DB i os) = i
	setLastID i (DB j os) = DB i os
	getObjects (DB i os) = os
	setObjects os (DB i ps) = DB i os
	
d0, d1, d2	::	DBS Object
d0 = empty
d1 = insert [("name", "john"),("age","30")] d0
d2 = insert [("name", "mary"),("age","20")] d1

test1	::	Object
test1	=	select 1 d1
test2	:: [Object]
test2	= selectBy (("john" ==).getName) d2

salka	::	ObjectSala
salka	=	objectSala 1 "S3C" 15 4

da0, da1, da2	::	DBS Object



